#!/usr/bin/env python
import argparse
import binascii
import sys
import socket
import random
import struct
import argparse
import hashlib
import datetime

from scapy.all import sendp, send, get_if_list, get_if_hwaddr, hexdump
from scapy.all import Packet
from scapy.all import Ether, IP, UDP, TCP
from scapy.all import *
from NDN_header_Interest import NDNInterest
from NDN_header_Data_v import NDNData

TYPE_NDN = 0x8624
content = '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'


def get_if(ifname):
    ifs = get_if_list()
    iface = None  # "h1-eth0"
    for i in get_if_list():
        if ifname in i:
            iface = i
            break
    if not iface:
        print("Cannot find " + ifname + " interface")
        exit(1)
    return iface


def main():
    pkt_type = 0
    parser = argparse.ArgumentParser()
    parser.add_argument('--i', type=str, help="The interface.")
    parser.add_argument('--name', type=str, help="The name of the interest or data package.")  # 4 components only
    parser.add_argument('--with_file', type=str, help="Set this argument to be a data package")
    args = parser.parse_args()

    # split name into 4 component: beijing0/bjtu0000/yulei000/index.ht 
    ndn_name = str(args.name)
    components = ndn_name.split('/', 4)
    # components_hex = [0x0, 0x0, 0x0, 0x0]
    components_hex = [0, 0, 0, 0]
    # print(components)
    # print(components[1])
    # print(binascii.b2a_hex(components[1]))
    for i in range(0, 3 + 1):
        # components_hex[i] = binascii.b2a (components[i])
        components_hex[i] = int(binascii.b2a_hex(components[i]), 16)
    # print (components_hex[0])
    content_hex = int(binascii.b2a_hex(content), 16)

    # others
    iface = get_if(args.i)
    if args.with_file is not None:
        pkt_type = 1
    print (pkt_type)

    if pkt_type:  # Data
        str_pkt_type = 'Data'
        bind_layers(Ether, NDNData, type=TYPE_NDN)
        s = hashlib.sha256('')
        s.update(components[0])
        s.update(components[1])
        s.update(components[2])
        s.update(components[3])
        s.update(content)
        # print(s)
        signatual_value = long(s.hexdigest(), 16)
        print("sending {} on interface {}".format(str_pkt_type, iface))
        pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff')
        # pkt = pkt / NDNData(Component1=components_hex[0], Component2=components_hex[1], Component3=components_hex[2],
       #                      Component4=components_hex[3], Content=content_hex, Signature_Value=signatual_value)
        pkt = pkt / NDNData(Component1=components_hex[0], Component2=components_hex[1], Component3=components_hex[2],
                             Content=content_hex, Signature_Value=signatual_value)
    else:  # interest
        str_pkt_type = 'interest'
        bind_layers(Ether, NDNInterest, type=TYPE_NDN)
        print("sending {} on interface {}".format(str_pkt_type, iface))
        pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff')
        # pkt = pkt / IP(dst=addr) / TCP(dport=1234, sport=random.randint(49152, 65535)) / args.message
        pkt = pkt / NDNInterest(Component1=components_hex[0], Component2=components_hex[1],
                                Component3=components_hex[2])

    pkt.show2()
    #    hexdump(pkt)
    #    print "len(pkt) = ", len(pkt)
    current_time = datetime.now()
    print("current_time:    " + str(current_time))
    sendp(pkt, count=1, iface=iface, verbose=False)


if __name__ == '__main__':
    main()
# example
# testApp
# randomData
# %FD%00%00%01%7D%80i%A6%D4
# Hello, world!
# sudo python sendndn.py --i enp27s0 --name beijing0/bjtu0000/yulei000/index.ht --with_file 3
